#pragma once

#include <list>
#include "MacroDefs.h"

#ifndef UINT
typedef unsigned int UINT;
#endif

class ICompHandler;
class IComponent
{
	protected:
		UINT	_hash;


	public:
		IComponent(): _hash(0) { }
		virtual ~IComponent() { }
		
		virtual bool OnInitResource(ICompHandler* handler) = 0;
		virtual bool OnFreeResource(ICompHandler* handler) = 0;

		void SetHash(UINT hash) { _hash = hash; }
		UINT GetHash() const { return this->_hash; }
};


class ICompHandler abstract
{
	private:
		UINT						_componentCount;
		std::list<IComponent*>*		_components;

	protected:
		ICompHandler()
		{
			_componentCount = 0;
			_components = new std::list<IComponent*>;
		}

	public:
		virtual ~ICompHandler() 
		{
			_componentCount = 0;

			if(_components != NULL)
				_components->clear();

			SAFE_DELETE(_components);
		}


		virtual void Attach(IComponent* comp)
		{
			_components->push_back(comp);
			comp->SetHash(this->_componentCount++);
		}


		virtual void Detach(IComponent* comp)
		{
			_components->remove(comp);
		}


		virtual bool OnFreeResources()
		{
			std::list<IComponent*>::iterator iter;

			for(iter = _components->begin(); iter != _components->end(); ++iter)
			{
				IComponent* c = (*iter);
				c->OnFreeResource(this);
			}

			return true;
		}


		virtual bool OnInitResources()
		{
			std::list<IComponent*>::iterator iter;

			for(iter = _components->begin(); iter != _components->end(); ++iter)
			{
				IComponent* c = (*iter);
				c->OnInitResource(this);
			}

			return true;
		}
};